﻿using System;
using System.Diagnostics;

public class PythonScriptRunner
{
    private string pythonExePath = @"D:\Anaconda3\envs\DLLpy38\python.exe"; // Python解释器的路径
    private string scriptPath = @"D:\download\Diffusion-Low-Light-main\processtest.py"; // Python脚本的路径
    private string workingPath=@"D:\download\Diffusion-Low-Light-main";
    public string ImagePath { set; get; }
    public string SavePath { set; get; }
    public PythonScriptRunner()
    {
        this.ImagePath = @"D:\HuaweiMoveData\Users\dlx\Desktop\rain_storm-003.jpg";
        this.SavePath = @"D:\HuaweiMoveData\Users\dlx\Desktop\restore";
    }
    public PythonScriptRunner(string imagePath, string savePath)
    {
        this.SavePath = savePath;
        this.ImagePath = imagePath;
    }

    public void RunArgs()
    {
        if (string.IsNullOrEmpty(ImagePath) || string.IsNullOrEmpty(SavePath))
        {
            throw new InvalidOperationException("Image path and save path must be set before running the script.");
        }

        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = pythonExePath,
            Arguments = $"\"{scriptPath}\" --image_path \"{ImagePath}\" --save_path \"{SavePath}\"",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true,
            WorkingDirectory = workingPath
        };

        using (Process process = new Process { StartInfo = startInfo })
        {
            process.Start();

            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();

            if (process.ExitCode == 0)
            {
                Console.WriteLine("Python script executed successfully.");
                Console.WriteLine("Output: " + output);
            }
            else
            {
                Console.WriteLine("Python script execution failed.");
                Console.WriteLine("Error: " + error);
            }
        }
    }
}
